const Net = require('../../common');
Page({
  data: {
    info: []
  },
  onLoad: function (options) {},
  onReady: function () {},
  onShow: function () {
    var _this = this
    _this.onPageListInfo()
  },
  onHide: function () {},
  onUnload: function () {},
  onPullDownRefresh: function () {},
  onReachBottom: function () {},
  onShareAppMessage: function () {},
  /**
   * 设置列表id默认
   */
  selectList(e) {
    var _this = this
    console.log({
      id: e.currentTarget.dataset.member
    })
    Net.link('v1/setDefaultMallAddress', {
        id: e.currentTarget.dataset.member
      },
      function (res) {
        console.log(res)
        if (res.data.code == 200) {
          Net.toast(data.msg, 'success')
          _this.onPageListInfo()
        }
      },
      function (res) {})
  },
  /**
   * 获取接口地址列表
   */
  onPageListInfo() {
    var _this = this
    Net.link('v1/mallAddress', {},
      function (res) {
        console.log(res)
        _this.setData({
          info: res.data.data.list
        })
      },
      function (res) {})
  },
  /**
   * 添加地址跳转
   */
  addAdress() {
    wx.navigateTo({
      url: '/pages/addAddress/addAddress',
    })
  },
  /**
   * 删除地址
   */
  onDeleteAddress(e) {
    var _this = this
    wx.showModal({
      title: '温馨提示',
      content: '是否删除该地址',
      success(res) {
        if (res.confirm) {
          Net.link('v1/mallAddress/' + e.currentTarget.id, {
              id: e.currentTarget.id
            },
            function (res) {
              console.log(res)
              if (res.data.data) {}
              Net.toast('删除成功', 'success')
              setTimeout(() => {
                _this.onPageListInfo()
              }, 1500);
            },
            function (res) {
              Net.toast(res.data.msg, 'none')
              console.log('报错')
            })
        }
      }
    })
  },
  //编辑收货地址
  edit: function (e) {
    var index = e.currentTarget.dataset.member;
    Net.PageRoter('_onCreateAddress', '?status=2' + '&id=' + index, 'nav')
  },
  /**
   * 选择收货地址
   */
  chooseAdress(e) {
    var data = {}
    data.realname = e.currentTarget.dataset.realname
    data.mobile = e.currentTarget.dataset.mobile
    data.region_path_name = e.currentTarget.dataset.region_path_name
    data.address = e.currentTarget.dataset.address
    data.id = e.currentTarget.dataset.id
    const pages = getCurrentPages();
    const prevPage = pages[pages.length - 2]
    var info = prevPage.data.info
    info.address = data
    console.log(info)
    prevPage.setData({
      info: info
    })
    wx.navigateBack({
      delta: 1
    })
  },
  onCreateAddress() {
    Net.PageRoter('_onCreateAddress', '', 'nav')
  }
})